package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierPlatDao;
import com.xb.loan.cashier.biz.dao.CashierPlatPaytypeDao;
import com.xb.loan.cashier.biz.dao.bean.CashierPlat;
import com.xb.loan.cashier.biz.service.CacheService;
import com.xb.loan.cashier.biz.service.CashierPlatService;
import com.xb.loan.cashier.biz.service.cache.RedisMethodCacheSupport;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import com.xb.loan.cashier.service.exception.ICashierServiceException;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.mybatis.MyTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("cashierPlatService")
public class CashierPlatServiceImpl implements CashierPlatService, CacheService {

    @Autowired
    private CashierPlatDao cashierPlatDao;
    
    @Autowired
    private CashierPlatPaytypeDao cashierPlatPaytypeDao;
    
    @Override
    public CashierPlat queryPlatById(String pid) {
        CashierPlat type = RedisMethodCacheSupport.getBean(CashierPlat.class, pid);
        if(type!=null){
            return type;
        }
        type = cashierPlatDao.queryPlatById(pid);
        if(type==null){
            return null;
        }
        RedisMethodCacheSupport.setBean(CashierPlat.class, type, pid);
        return type;
    }

    @Override
    public void clearCache(String methodName) {
        RedisMethodCacheSupport.clear(methodName);
    }

    @Override
    public void clearCacheAll() {
        RedisMethodCacheSupport.clearAll();
    }

	@Override
	public List<CashierPlat> queryPlat() {
		return cashierPlatDao.queryPlat();
	}

	@Override
	@MyTransactional
	public void saveOrUpdateCashierPlat(String type, CashierPlat cpc,
                                        String[] payGateWay) throws BizException {
		int res = 0;
		if (cpc==null) {
			throw ICashierServiceException.inputParameterIsWrong;
		}
		CashierPlat cp = cashierPlatDao.queryPlatByPlatName(cpc.getPlatName());
		if ("save".equals(type)) {
			if (cp!=null) {
				throw ICashierServiceException.PLAT_NAME_EXIST;
			}
			String platIdAdd = FormatOperation.assemblySeq(
					ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PLAT.getCode(),
					ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PLAT.getNum(),
					FormatOperation.formatDateToYMDHms(new Date()), "");
			cpc.setPlatId(platIdAdd);
			res = cashierPlatDao.insertSelective(cpc);
			if (res<1) {
				throw ICashierServiceException.systemException;
			}
			
		} else if ("update".equals(type)) {
			CashierPlat cashierPlat = cashierPlatDao.queryCashierPlatById(cpc.getPlatId());
			if (cashierPlat!=null && !cashierPlat.getPlatName().equals(cpc.getPlatName())) {//验证是否修改过名称
				if (cp!=null) {//修改过名称且能查到,表示已存在
					throw ICashierServiceException.PLAT_NAME_EXIST;
				}
			}
			
			res = cashierPlatDao.deletePlatpaytypeById(cpc.getPlatId());
			if (res<1) {
				throw ICashierServiceException.systemException;
			}
		} else {
			throw ICashierServiceException.inputParameterIsWrong;
		}
		if(payGateWay != null && payGateWay.length > 0){
			for(String platpaytype : payGateWay){
				int paychannelType = cashierPlatDao.queryPaychannelType(platpaytype);
				String platChannelId = FormatOperation.assemblySeq(
						ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PLATPAYTYPE.getCode(),
						ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PLATPAYTYPE.getNum(),
						FormatOperation.formatDateToYMDHms(new Date()), "");
				Map<String, Object> maptype = new HashMap<String, Object>();
				maptype.put("platChannelId", platChannelId);
				maptype.put("platId", cpc.getPlatId());
				maptype.put("channelId", platpaytype);
				maptype.put("encryptType", 0);
				maptype.put("paychannelType", paychannelType);
				res = cashierPlatDao.insertPlatPayType(maptype);
				if (res<1) {
					throw ICashierServiceException.systemException;
				}
			}
		}else {
			throw ICashierServiceException.inputParameterIsWrong;
		}
		
	}

	@Override
	public List<CashierPlat> queryCashierPlatListAll(){
		return cashierPlatDao.queryCashierPlatListAll();
	}
	
	@Override
	public int queryCashierPlatCount() {
		return cashierPlatDao.queryCashierPlatCount();
	}
	@Override
	@MyTransactional
	public void deleteCashierPlat(String platId) throws BizException{
		int res = cashierPlatDao.deleteByPrimaryKey(platId);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
		res = cashierPlatPaytypeDao.deleteByPlatId(platId);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
	}
	@Override
	public void updateCashierPlatStatus(String platId, String enbale) throws BizException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("platId", platId);
		map.put("enbale", Integer.valueOf(enbale));
		int res = cashierPlatDao.updateCashierPlatStatus(map);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
	}

}
